<?php

/**
 * 默认控制器
 */
class Controller_Htmltemplate extends Controller_Abstract
{
    function actionIndex()
    {
    	$page = $this->_context->get('page', 1);
    	$filter = $this->_context->get('filter', null);
    	
    	$htmlTemplates = Htmltemplates::model()->paginationGetAll($page, $filter);
    	$pagination = new Pagination($htmlTemplates['totalCount'], $this->_context->requestUri(), $page, HTMLTEMPLATES_LIST_PERPAGE);
		unset($htmlTemplates['totalCount']);;
    	
    	$this->_view['htmlTemplates'] = $htmlTemplates;
    	$this->_view['pagination'] = $pagination->render();
    	
		$this->_view['filter'] = $filter;
    	
    	$allTypes = Types::model()->find('*', array('tablename' => 'htmltemplates'), array('setResultKey' => 'id'));
    	$this->_view['beusedTypeAreas'] = dataUtil::getElementByKey(
    		$allTypes, 
    		Htmltemplates::model()->find('type_area_id', null, array('distinct' => true, 'stripName' => true))
    	);
    	$this->_view['beusedTypeColors'] = dataUtil::getElementByKey(
    		$allTypes, 
    		Htmltemplates::model()->find('type_color_id', null, array('distinct' => true, 'stripName' => true))
    	);
		$this->_view['beusedTypeColumnWidths'] = dataUtil::getElementByKey(
			$allTypes, 
			Htmltemplates::model()->find('type_columnwidth_id', null, array('distinct' => true, 'stripName' => true))
		);
		$this->_view['beusedTypeSidebarCorners'] = dataUtil::getElementByKey(
			$allTypes, 
			Htmltemplates::model()->find('type_sidebarcorner_id', null, array('distinct' => true, 'stripName' => true))
		);
		$this->_view['beusedTypeUsings'] = dataUtil::getElementByKey(
			$allTypes, 
			Htmltemplates::model()->find('type_using_id', null, array('distinct' => true, 'stripName' => true))
		);
		
		$this->_view['crumbs'] = array(
			'Htmltemplate模板' => ''
		);
    }
    
    /*function actionDetail()
    {
    	$id = $this->_context->get('id', null);
    	if($id){
    		$htmlTemplatesModel = new Htmltemplates();
    		$htmlTemplate = $htmlTemplatesModel->find('*', array('id' => $id), array('onlyOne' => true));
    		
    		if(empty($htmlTemplate)){
    			die('没找到相应ID的html模板,跳转到404页面');
    			exit();
    		}
	    	if(file_exists(iconv('UTF-8', 'GB2312', HTMLTEMPLATES_FOLDER . DIRECTORY_SEPARATOR . $htmlTemplate['root_foldername'] . DIRECTORY_SEPARATOR . $htmlTemplate['download_pack']))){
	    		$this->_view['download_pack'] = $htmlTemplate['download_pack'];
	    	}
    		
    		$this->_view['htmlTemplate'] = $htmlTemplate;
    	}
    }*/
    
    function actionPreview()
    {
    	$id = $this->_context->get('id', null);
    	$htmlTemplatesModel = new Htmltemplates();
    	$htmlTemplate = $htmlTemplatesModel->find('*', array('id' => $id), array('onlyOne' => true));
    	
    	if(!empty($htmlTemplate) && !file_exists(iconv('UTF-8', 'GB2312', HTMLTEMPLATES_FOLDER . DIRECTORY_SEPARATOR . $htmlTemplate['root_foldername'] . DIRECTORY_SEPARATOR . $htmlTemplate['preview_indexpage']))){
	    	die('没有找到详细预览页面，跳转到404页面');
	    	exit();
    	}
    	
    	if(file_exists(iconv('UTF-8', 'GB2312', HTMLTEMPLATES_FOLDER . DIRECTORY_SEPARATOR . $htmlTemplate['root_foldername'] . DIRECTORY_SEPARATOR . $htmlTemplate['download_pack']))){
    		$this->_view['download_pack'] = $htmlTemplate['download_pack'];
    	}
    	$this->_view['id'] = $id;
    	$this->_view['root'] = $htmlTemplate['root_foldername'];
    	$this->_view['indexpage'] = $htmlTemplate['preview_indexpage'];
    }
    
    protected function _after_execute(& $response)
    {
    	$noRightColumnActions = array('preview');
    	if(!in_array($this->_context->action_name, $noRightColumnActions)){
	    	$this->_view['recommends'] = Htmltemplates::model()->getRecommends();
	    	$this->_view['downloads'] = Htmltemplates::model()->getTopDownloads();
	    	$this->_view['vieweds'] = Htmltemplates::model()->getTopViews();
    	}
    	
    	parent::_after_execute($response);
    }
}

